<?php

class CuentasProyectosController extends Sgit_Controller_Action
{


    protected $_checkZona = true;

    protected $_tipo = 'Electricidad';


    
    
    public function tablas()
    {
        

        $fecha = new Zend_Date();
		$fecha->setDay(1)->setMonth(4)->setYear(2012);
		
        $this->_helper->layout->disableLayout();
        
        $cuentas = new Application_Model_Proyectos();
        $tables= $cuentas->query('SHOW TABLES');
        $i=0;
        
        foreach($tables as $v)
        {
            $i++;
            if($i>300)continue;
            
            $tabla = $cuentas->getAdapter()->quoteIdentifier($v['Tables_in_sgit']);
            
            $info[$tabla]= $cuentas->query('DESCRIBE '.$tabla);
            
            
        }




        $ruta=APPLICATION_PATH."/tmp/";

     
        $fp = fopen($ruta."tablas.csv", 'w');
     

        $header=array("Tabla","Field","Type","Null","Key","Default","Extra");
        
        
       $delimiter = ';';
       $enclosure = '"';
        
       
        fputcsv($fp, $header,$delimiter,$enclosure);

        foreach($info as $tabla => $data)
        {
            $tabla=str_replace('`','',$tabla);
            
            $h_tabla=array($tabla ,"","","","","","");
            fputcsv($fp, $h_tabla,$delimiter,$enclosure);
            
            
            foreach ($data as  $key=>$campos) {
                array_unshift($campos,'');
                fputcsv($fp, $campos,$delimiter,$enclosure);
            }
            
            $h_tabla=array("","" ,"" ,"" ,"" ,"" ,"");
            fputcsv($fp, $h_tabla,$delimiter,$enclosure);
            fputcsv($fp, $h_tabla,$delimiter,$enclosure);
                
              
        }


        fclose($fp);
        
        exit();
        

        $this->view->info=$info;
        
    }
    
    
    
    
    
    public function rectaElectricidadAction()
    {
        
      
        $this->_helper->layout->disableLayout();
        
        $id_pep= $this->_getParam('id_pep');

        $cuentas = new Application_Model_InformeElectricidadMensualProyecto();
        $regresion=$cuentas->getProgresion($id_pep,$this->_idZona,'consumo_it');

        $precios=new  Application_Model_InformeElectricidadMensualProyecto();
        $precio = $precios->precio_estimado($id_pep,$this->_idZona);
        

        $f=new Zend_Date();
        foreach($regresion as &$array)
        {
            foreach($array as $fecha => &$value)
            {
                $mes= $f->set($fecha)->subMonth(1)-> get('M');
                $horas_mes=$f->get(Zend_Date::MONTH_DAYS)*24;
                
                $value=$value ? $value * $horas_mes* $precio[$mes]: null;
            }
        }
        Zend_Debug::dump($regresion);

        $this->view->regresion=$regresion;
        
        $this->render('recta');

    }

     public function rectaAlojamientoAction()
    {
        
      
        $this->_helper->layout->disableLayout();
        
        $id_pep= $this->_getParam('id_pep');

        $cuentas = new Application_Model_InformeAlojamientoMensualProyecto();
        $regresion=$cuentas->getProgresion($id_pep,$this->_idZona,'imputacion_alojamiento',8);

        foreach($regresion as &$array)
        {
            foreach($array as $fecha => &$value)
            {
                $value=$value ? $value: null;
            }
        }
        
        
        Zend_Debug::dump($regresion);

        $this->view->regresion=$regresion;
        
        $this->render('recta');

    }
    
   
    public function rectaMmrAction()
    {

        $this->_helper->layout->disableLayout();
        
        $id_pep= $this->_getParam('id_pep');

        $cuentas = new Application_Model_InformeMMRMensualProyecto();
        $regresion=$cuentas->getProgresion($id_pep,$this->_idZona,'coste',8);

        foreach($regresion as &$array)
        {
            foreach($array as $fecha => &$value)
            {
                $value=$value ? $value: null;
            }
        }
        
        Zend_Debug::dump($regresion);
        $this->view->regresion=$regresion;
        
        $this->render('recta');

    } 
    
    
    
    public function costesAction()
    {

        $this->setParam('anio',date('Y'));

        $anio = $this->_getParam('anio');
        $id_pep = $this->_getParam('id_pep');

        $m_pep = new Application_Model_Pep();
        $this->view->pep=$m_pep->getPeps($id_pep);
       

        $filtros = new Sgit_Form_Filtros();
        $f_anio = new Sgit_Form_Element_Select_Year('anio');
        $filtros->addFormSelect($f_anio);
        $values = $this->_getFiltrosValues($filtros);
        $filtros->myRender();

        if ($anio)
        {
            
            $cuentas = new Application_Model_CuentasProyectos();
            
            $cuentas->setAnio($anio);
            $cuentas->setPep($id_pep);
            $cuentas->setZona($this->_idZona);
            $this->view->anio = $anio;
            $this->view->id_pep = $id_pep;
             
            //costes
            $this->view->financieros =  $cuentas->getDatosCategoriaMeses(1);
            $this->view->varios =       $cuentas->getDatosCategoriaMeses(2);
            $this->view->mano_obra =    $cuentas->getDatosCategoriaMeses(3);
            $this->view->electricidad = $cuentas->getDatosCategoriaMesesForecast(4);
            $this->view->alojamiento =  $cuentas->getDatosCategoriaMesesForecast(5);
            $this->view->mmr =          $cuentas->getDatosCategoriaMesesForecast(6);
            $this->view->obras =        $cuentas->getDatosCategoriaMeses(7);
            $this->view->backup =       $cuentas->getDatosCategoriaMeses(8);
            $this->view->internet =     $cuentas->getDatosCategoriaMeses(9);
            $this->view->monitor =      $cuentas->getDatosCategoriaMeses(10);
            
            $this->_pdfSubTitle = $this->zona;
            $this->_export($this->view->rows);
            
            $xml = $this->_getXml($this->_actionName);
            $this->view->columnas = $xml['columnas'];
            
        }

    }


    public function editarAction()
    {
        $value = Sgit_Filter_FloatNull::filter($this->_getParam('value'));
        $idZona =      $this->_getParam('idZona');
        $id_categoria =$this->_getParam('id_categoria');
        $id_pep =      $this->_getParam('id_pep');
        $mes =         $this->_getParam('mes');
        $anio =        $this->_getParam('anio');
 
        $keys=array('id_zona'=>$idZona,'id_categoria'=>$id_categoria,'id_pep'=>$id_pep,'mes'=>$mes,'anio'=>$anio);
        $forUpdate=array('value'=>$value);
        
        $cuentas = new Application_Model_CuentasProyectos();
        $cuentas->replace($keys, $forUpdate);

        $where['id_zona=?']     = $idZona;
        $where['id_categoria=?']= $id_categoria;
        $where['anio=?']        = $anio;
        $where['id_pep=?']      = $id_pep;

        $suma=$cuentas->getAssoc("mes","value", $where);
        $sumaq1=$suma[1]+$suma[2]+$suma[3];
        $sumaq2=$sumaq1+$suma[4]+$suma[5]+$suma[6];
        $sumaq3=$sumaq2+$suma[7]+$suma[8]+$suma[9];
        $sumaq4=$sumaq3+$suma[10]+$suma[11]+$suma[12];
            
        $json[0]['funcion']='val';
        $json[0]['select']="#$id_categoria-q1";
        $json[0]['value']=round($sumaq1);
        
        $json[1]['funcion']='val';
        $json[1]['select']="#$id_categoria-q2";
        $json[1]['value']=round($sumaq2);
        
        $json[2]['funcion']='val';
        $json[2]['select']="#$id_categoria-q3";
        $json[2]['value']=round($sumaq3);
        
        $json[3]['funcion']='val';
        $json[3]['select']="#$id_categoria-q4";
        $json[3]['value']=round($sumaq4);
        
         
        $this->_helper->json($json);

    }



       
}





